package cn.lj.logistics.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.tags.EditorAwareTag;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.lj.logistics.pojo.MsgObject;
import cn.lj.logistics.pojo.Order;
import cn.lj.logistics.pojo.OrderDetail;
import cn.lj.logistics.pojo.OrderExample;
import cn.lj.logistics.pojo.Order;
import cn.lj.logistics.pojo.OrderExample;
import cn.lj.logistics.pojo.OrderExample.Criteria;
import cn.lj.logistics.pojo.User;
import cn.lj.logistics.pojo.UserExample;
import cn.lj.logistics.service.BaseDataService;
import cn.lj.logistics.service.CustomerService;
import cn.lj.logistics.service.OrderService;
import cn.lj.logistics.service.UserService;
import cn.lj.logistics.service.OrderService;
import net.sf.jsqlparser.statement.delete.Delete;

@Controller
@RequestMapping("/order")
public class OrderController {
     
	@Autowired
	private OrderService orderService;
	
	@Autowired UserService userService;
	
	@Autowired
	private CustomerService customerService;

	@Autowired
	private BaseDataService baseDataService;
	//编辑页面，新增和修改公用的页面
	@RequestMapping("/edit")
	public String edit(Model m,Long orderId) {
		
		//修改操作
		if (orderId!=null) {
			// 查询出当前用户的数据，共享给编辑页面，以供修改回显数据
			Order order = orderService.selectByPrimaryKey(orderId);
			m.addAttribute("order",order);
		}
	
		return "orderEdit";
		
	}
	

	
	//新增功能
		@RequestMapping("/insert")
		@ResponseBody
		public MsgObject insert(Order order) {
			
			
		
		int row = orderService.insert(order);
			MsgObject mo = MsgObject.createMO(0, "新增数据失败，请联系管理员！！！");
			if (row==1) {
				mo=MsgObject.createMO(1, "新增数据成功");
			}
			return mo;
			
		}
		
		//修改功能
				@RequestMapping("/update")
				@ResponseBody
				public MsgObject update(Order order) {
				
				
				int row = orderService.updateByPrimaryKeySelective(order);
					MsgObject mo = MsgObject.createMO(0, "修改数据失败，请联系管理员！！！");
					if (row==1) {
						mo=MsgObject.createMO(1, "修改数据成功");
					}
					return mo;
					
				}
		
	
	//删除功能
	@RequestMapping("/delete")
	@ResponseBody
	public MsgObject delete(Long orderId) {
		
		

		MsgObject mo = MsgObject.createMO(0, "删除数据失败，请联系管理员！！");
	
		
		int row = orderService.deleteByPrimaryKey(orderId);
	
		if (row==1) {
			mo=MsgObject.createMO(1, "删除数据成功");
		}
		return mo;
		
	}
	

	
	@RequestMapping("/orderPage")
	@RequiresPermissions("order:orderPage")
	public String orderPage() {
		
		return "orderPage";
		
	}
	

	@RequestMapping("/list")
	@RequiresPermissions("order:list")
	@ResponseBody
	public PageInfo<Order> list(@RequestParam(defaultValue = "1")Integer pageNum,
			               @RequestParam(defaultValue = "10")Integer pageSize ,String keyword){
		System.out.println("keyword:"+ keyword);
		//开始分页
		PageHelper.startPage(pageNum, pageSize);
		OrderExample example=new OrderExample();
		
		if (StringUtils.isNotBlank(keyword)) {
			// 创建限制对象
			Criteria criteria1 = example.createCriteria();
		criteria1.andShippingNameLike("%"+keyword+"%");

	
		}
		List<Order> list = orderService.selectByExample(example);
		
		return  new PageInfo<Order>(list);
		
	}
	
	/* 根据订单的id查询出对应的订单明细数据 */
	@RequestMapping("/detail")
	@ResponseBody
	public List<OrderDetail> details (Long orderId){
		
		List<OrderDetail> orderDetails = orderService.selectOrderDetailsByOrderId(orderId);
		return orderDetails;
		
		
		
	}
	
}
